package com.xc.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by xc
 */
@Slf4j
public class DateTimeUtil {

    //joda-time

    //str->Date
    //Date->str
    public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";


    public static Date getCurrentDate(){
        return new Date();
    }


    public static Date strToDate(String dateTimeStr, String formatStr){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }


    public static String dateToStr(Date date, String formatStr){
        if(date == null){
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(formatStr);
    }

    public static Date strToDate(String dateTimeStr){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }
    public static Timestamp searchStrToTimestamp(String dateTimeStr){
        Timestamp time = Timestamp.valueOf(dateTimeStr);
        return time;
    }

    public static String dateToStr(Date date){
        if(date == null){
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(STANDARD_FORMAT);
    }

    //  时间戳转date
    public static Date longToDate(Long time){
        SimpleDateFormat format = new SimpleDateFormat(STANDARD_FORMAT);

        String d = format.format(time);

        Date date = null;
        try {
            date = format.parse(d);
        }catch (Exception e){
            log.error("datetime utils longToDate error");
        }
        return date;
    }


    //  计算到期时间 (传入的是毫秒，要转化成秒)
    public static Date doEndTime(Date begintime, int month){

        Long begintimelong = begintime.getTime()/1000;
        log.info("计算时间 传入时间 = {} , 时间戳 = {}", DateTimeUtil.dateToStr(begintime) , begintimelong);

        Long endtimelong = begintimelong + 86400 * 30 * month;
        Date endtimedate = DateTimeUtil.longToDate(endtimelong * 1000);

        log.info("endtime 时间戳 = {},时间 = {} , 格式化时间={}",endtimelong , endtimedate,
                DateTimeUtil.dateToStr(endtimedate));

        return endtimedate;
    }


    public static String getCurrentTimeMiao(){
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        return timestamp;
    }



    public static void main(String[] args) {
//        System.out.println(DateTimeUtil.dateToStr(new Date(),"yyyy-MM-dd HH:mm:ss"));
//        System.out.println(DateTimeUtil.strToDate("2010-01-01 11:11:11","yyyy-MM-dd HH:mm:ss"));


//        DateTimeUtil.doEndTime(DateTimeUtil.getCurrentTime(),2);


//        System.out.println(Timestamp.valueOf(DateTimeUtil.dateToStr(new Date())));


//        String str = DateTimeUtil.dateToStr(new Date(),"yyyyMMDDhhmmss");
//        System.out.println(str);
//
//
//        System.out.println(searchStrToTimestamp("2018-11-13 00:00:00"));


        String cu = getCurrentTimeMiao();
        System.out.println(cu);

        long cuc = Long.parseLong(cu) * 1000;
        System.out.println(cuc);

        System.out.println(DateTimeUtil.dateToStr(DateTimeUtil.longToDate(cuc)));


    }


}
